//
//  GYModifyViewController.m
//  research
//
//  Created by 章鱼 on 17/3/30.
//  Copyright © 2017年 simon. All rights reserved.
//

#import "GYModifyViewController.h"
#import "GYRegisterTextfieldView.h"
#import "GYLoginViewController.h"

@interface GYModifyViewController ()
@property (nonatomic, strong) GYRegisterTextfieldView *oldPassWOrdView;//旧密码
@property (nonatomic, strong) GYRegisterTextfieldView *newsPassWordView;//新密码
@property (nonatomic, strong) GYRegisterTextfieldView *surePasswordView;//确认密码

@property (nonatomic, strong) UIButton *finishedButton;

@end

@implementation GYModifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self basicSetup];
    [self initModifyView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self addBackButtonItemWithTitle:@"返回" backImg:@"navigation-bar_back_normal@2x"];
}

- (void)basicSetup {
    [self initNavBarWithTitle:@"修改密码" tintColor:[UIColor orz_mainColor]];
    [self.navBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                         [UIColor whiteColor],NSForegroundColorAttributeName,
                                         [UIFont boldSystemFontOfSize:20],
                                         NSFontAttributeName,
                                         nil]];
    [self.view setBackgroundColor:[UIColor orz_globalBackground]];
    
    
}


-(void)initModifyView {
    _oldPassWOrdView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_oldPassWOrdView];
    _oldPassWOrdView.titleLable.text = @"旧密码";
    _oldPassWOrdView.textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    _oldPassWOrdView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请您正确输入当前密码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _newsPassWordView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_newsPassWordView];
    _newsPassWordView.titleLable.text = @"新密码";
    _newsPassWordView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入2～9的密码" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _surePasswordView = [[GYRegisterTextfieldView alloc] init];
    [self.view addSubview:_surePasswordView];
    _surePasswordView.titleLable.text = @"确认密码";
    _surePasswordView.textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"为了保险请再来一遍" attributes:@{NSForegroundColorAttributeName:[UIColor orz_textWhiteBackground]}];
    
    _finishedButton = [[UIButton alloc] init];
    [self.view addSubview:_finishedButton];
    [_finishedButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _finishedButton.backgroundColor = [UIColor orz_mainColor];
    [_finishedButton setTitle:@"完成" forState:UIControlStateNormal];
    _finishedButton.titleLabel.font = [UIFont systemFontOfSize:14];
    _finishedButton.layer.cornerRadius = 4.f;
    _finishedButton.layer.masksToBounds = YES;
    [_finishedButton addTarget:self action:@selector(finishedClick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self initModifyViewUI];
}

-(void)initModifyViewUI {
    
    [_oldPassWOrdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(84.f);
        make.left.mas_offset(0.f);
        make.right.mas_offset(0.f);
        make.height.mas_offset(44.f);
        
    }];

    [_newsPassWordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(128.f);
        make.left.mas_offset(0.f);
        make.right.mas_offset(0.f);
        make.height.mas_offset(44.f);
        
    }];
    
    [_surePasswordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(172.f);
        make.left.mas_offset(0.f);
        make.right.mas_offset(0.f);
        make.height.mas_offset(44.f);
        
    }];
    
    [_finishedButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_surePasswordView.mas_bottom).offset(20.f);
        make.left.mas_equalTo(12.f);
        make.right.mas_equalTo(-12.f);
        make.height.mas_equalTo(44.f);
    }];
    
}


- (void)finishedClick:(UIButton *)btn {

    if (_oldPassWOrdView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入当前密码"];
        return;
    }
    if (_newsPassWordView.textFiled.text.length == 0) {
        [VBAlertView showTextHUD:@"请输入新密码"];
        return;
    }
    if (![_newsPassWordView.textFiled.text isEqualToString:_surePasswordView.textFiled.text]) {
        [VBAlertView showTextHUD:@"请两次输入同样的密码"];
        return;
    }
    [self userIsLogin];
    [[YGHttpManager defaultManager] fetchGetUserModifyPasswdWithToken:[[GYConfigDBManager defaultManager] gyToken]
                                                               userId:[NSString stringWithFormat:@"%ld",(long)[[GYConfigDBManager defaultManager] userInformation].id]
                                                            newPasswd:_newsPassWordView.textFiled.text oldPasswd:_oldPassWOrdView.textFiled.text success:^(id responseObj) {
                                                                GYCommonModel *commonmodel = [GYCommonModel mj_objectWithKeyValues:responseObj];
                                                                if (commonmodel.status ==1) {
                                                                    [VBAlertView showTextHUD:@"修改密码成功"];
                                                                    [self.navigationController popViewControllerAnimated:YES];
                                                                }
                                                                if (commonmodel.status ==0) {
                                                                    [VBAlertView showTextHUD:commonmodel.message];
                                                                }
                                                                
                                                            } failure:^(NSError *error) {
                                                                
                                                            }];

}

#pragma mark - action 判断是否登录
- (void)userIsLogin {
    
    [[YGHttpManager defaultManager] fetchGetUserIsloginWithtoken:[[GYConfigDBManager defaultManager] gyToken] success:^(id responseObj) {
        //重新登录
        if ([[responseObj objectForKey:@"status"] integerValue] == 0) {
            [VBAlertView showTextHUD:[responseObj objectForKey:@"message"]];
            GYLoginViewController *loginCtrl = [[GYLoginViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginCtrl];
            [self presentViewController:nav animated:YES completion:NULL];
            
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
